#include "trayicon.h"
#include <QtWidgets/QApplication>
#include "mainwindow.h"

TrayIcon::TrayIcon(QObject *parent, QIcon* icon) :
  QSystemTrayIcon(parent)
{
  if(icon != NULL)
    setIcon(*icon);
  setToolTip(tr("Rift Tool App"));
  createMenu();
  setContextMenu(menu);
  connect(this, SIGNAL(messageClicked()), parent, SLOT(showEventList()));
  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(activate(QSystemTrayIcon::ActivationReason)));
  connect(this, SIGNAL(showUp()), parent, SLOT(showUp()));
}

TrayIcon::~TrayIcon()
{
  menu->deleteLater();
}

void TrayIcon::activate(QSystemTrayIcon::ActivationReason reason)
{
  if(reason == DoubleClick)
    emit showUp();
}

void TrayIcon::createMenu()
{
  menu = new QMenu;
  createActions();

  menu->addAction(simulatorAction);
  menu->addAction(eventAction);
  menu->addAction(chatAction);
  menu->addSeparator();
  menu->addAction(optionAction);
  menu->addAction(quitAction);
}

void TrayIcon::createActions()
{
  quitAction = new QAction(tr("Quit"), menu);
  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

  simulatorAction = new QAction(tr("Simulator"), menu);
  connect(simulatorAction, SIGNAL(triggered()), parent(), SLOT(showSimulator()));

  eventAction = new QAction(tr("Events list"), menu);
  connect(eventAction, SIGNAL(triggered()), parent(), SLOT(showEventList()));

  chatAction = new QAction(tr("Chat"), menu);
  connect(chatAction, SIGNAL(triggered()), parent(), SLOT(showChat()));

  optionAction = new QAction(tr("Options"), menu);
  connect(optionAction, SIGNAL(triggered()), parent(), SLOT(showOptions()));
}
